package com.saturn.titan.core.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.saturn.titan.core.bean.BusiWorkBill;
import com.saturn.titan.core.comm.BillType;
import com.saturn.titan.core.dto.AppWorkBillDto;
import com.saturn.titan.core.dto.WorkBillDto;
import com.saturn.titan.core.repository.mybatis.BusiWorkBillDao;
import com.saturn.titan.web.utils.CRMUtil;

@Component
@Transactional(readOnly = true)
public class WorkBillService {

	@Autowired
	private BusiWorkBillDao busiWorkBillDao;
	
	@Autowired
	private BillNoService billNoService;

	@Transactional(readOnly = false)
	public void insert(BusiWorkBill bill){
		
		bill.setOrgId(CRMUtil.getCurrentUserOrgId());
		
		CRMUtil.setBusiBillOprFileForInsert(bill);
		
		bill.setBillNo(billNoService.getNextBillNo(BillType.WB));
		
		busiWorkBillDao.insert(bill);
	}
	
	public BusiWorkBill findById(Long id){
		return busiWorkBillDao.selectById(id);
	}

	public List<AppWorkBillDto> getAllWork(Long empId) {
		return busiWorkBillDao.getNoExecWorkBill(empId);
	}
	
	public List<AppWorkBillDto> getReciveWork(Long empId) {
		return busiWorkBillDao.getReciveWorkBill(empId);
	}
	
	public List<BusiWorkBill> getAllWorkBill(BusiWorkBill bill) {
		return busiWorkBillDao.select(bill);
	}
	
	
	public List<WorkBillDto> getAllWorkBillInfo(String customerPhone,
			String billStt ,
			String requestType,
			String emergentType) {
		return busiWorkBillDao.getAllWorkBillInfo(customerPhone,billStt,requestType,emergentType);
	}
	
	@Transactional(readOnly = false)
	public int  update(BusiWorkBill bill){
		int result = busiWorkBillDao.update(bill);
		return result;
	}
	
	@Transactional(readOnly = false)
	public void cancle(Long id) {
		BusiWorkBill bill = busiWorkBillDao.selectById(id);
		bill.setBillStt(9);
		busiWorkBillDao.update(bill);
	}
}
